// let THREE = require('three');
import * as THREE from "three";
import { OrbitControls } from "./vendor/threejs/controls/OrbitControls.js";
import { TOOLS } from "./app/tools/tools.js";
import { CONFIG } from "./app/config.js";
import { OBJECTS, UTILS, FLAGS } from "./app/declarations.js";
import { PRELOADER } from "./app/preloader.js";
import { POSTPROCESSING } from "./app/world/postprocessing.js";
import { UI } from "./app/ui/ui.js";

import * as $ from "jquery";
const qs = require("query-string");

// let controlsPicked = false

// let deviceType = TOOLS.getAgent()

let APP = {
  init: () => {
    APP.ready();
  },

  ready: () => {
    console.log("ready");

    let _qs = qs.parse(location.search);

    const aspect = window.innerWidth / window.innerHeight;
    const frustumSize = CONFIG.camera.world.frustumSize;
    OBJECTS.container = document.getElementById("viewContainer");

    // create the two cameras, one for ui one for the webgl world

    if (CONFIG.camera.world.type == "orthographic") {
      OBJECTS.camera = new THREE.OrthographicCamera(
        (frustumSize * aspect) / -2,
        (frustumSize * aspect) / 2,
        frustumSize / 2,
        frustumSize / -2,
        1,
        CONFIG.camera.world.far
      );
      // OBJECTS.camera= new THREE.OrthographicCamera( 0.5 * frustumSize * aspect / - 2, 0.5 * frustumSize * aspect / 2, frustumSize / 2, frustumSize / - 2, 150, 10000 );
    } else {
      OBJECTS.camera = new THREE.PerspectiveCamera(
        CONFIG.camera.world.viewAngle,
        UTILS.sw / UTILS.sh,
        CONFIG.camera.world.near,
        CONFIG.camera.world.far
      );
    }

    OBJECTS.camera.fov = CONFIG.camera.world.fov;
    OBJECTS.camera.position.copy(CONFIG.camera.world.position);
    OBJECTS.camera.rotation.x = THREE.Math.degToRad(-90);
    OBJECTS.camera.zoom = CONFIG.camera.world.zoom;

    OBJECTS.cam_target = new THREE.Object3D();
    OBJECTS.cam_target.name = "cam_target";
    OBJECTS.cam_target.position.copy(CONFIG.camera.world.target);

    OBJECTS.camera.updateProjectionMatrix();

    OBJECTS.renderer = new THREE.WebGLRenderer({
      clearColor: CONFIG.renderer.clearColor,
      alpha: CONFIG.renderer.alpha,
      antialias: CONFIG.renderer.antialias,
      preserveDrawingBuffer: CONFIG.renderer.preserveDrawingBuffer,
    });
    window.renderer = OBJECTS.renderer;

    OBJECTS.currentCamera = OBJECTS.camera;
    OBJECTS.cameras.push(OBJECTS.camera);

    OBJECTS.renderer.autoClear = false;
    OBJECTS.uiCamera = new THREE.PerspectiveCamera(
      CONFIG.camera.ui.viewAngle,
      UTILS.sw / UTILS.sh,
      CONFIG.camera.ui.near,
      CONFIG.camera.ui.far
    );

    OBJECTS.uiCamera.fov = CONFIG.camera.ui.fov;
    OBJECTS.uiCamera.position.copy(CONFIG.camera.ui.position);
    OBJECTS.uiCamera.target = OBJECTS.cam_target.position;

    OBJECTS.renderer.setClearColor(CONFIG.renderer.clearColor, 0);

    OBJECTS.renderer.shadowMap.enabled = CONFIG.renderer.shadowMap.enabled;
    // OBJECTS.renderer.shadowMap.enabled = CONFIG.renderer.shadowMap.enabled
    // OBJECTS.renderer.setPixelRatio(window.devicePixelRatio)
    OBJECTS.renderer.setPixelRatio(CONFIG.renderer.pixelRatio);
    OBJECTS.container.appendChild(OBJECTS.renderer.domElement);

    for (var key in CONFIG.scenes) {
      let sc = CONFIG.scenes[key];
      OBJECTS.scenes[key] = new THREE.Scene();
      OBJECTS.scenes[key].name = CONFIG.scenes[key].name;
      OBJECTS.scenes[key].render = CONFIG.scenes[key].render;
    }

    if (CONFIG.renderer.toneMapping) {
      OBJECTS.renderer.toneMapping = CONFIG.renderer.toneMapping;
      // console.log(">>>"+CONFIG.renderer.toneMapping)
    }

    if (CONFIG.camera.controls.enabled) {
      if (CONFIG.camera.controls.type == "deviceOrientation") {
        OBJECTS.camera.controls = new DeviceOrientationControls(OBJECTS.camera);
        OBJECTS.camera.controls.connect();
        // console.log(OBJECTS.camera.controls)
      }
      if (CONFIG.camera.controls.type == "orbitControls") {
        OBJECTS.camera.controls = new OrbitControls(
          OBJECTS.camera,
          OBJECTS.renderer.domElement
        );
        OBJECTS.camera.controls.target = OBJECTS.cam_target.position;
        OBJECTS.camera.controls.enableZoom = CONFIG.camera.controls.enableZoom;
        OBJECTS.camera.controls.enablePan = CONFIG.camera.controls.enablePan;
        OBJECTS.camera.controls.enableRotate =
          CONFIG.camera.controls.enableRotate;
        OBJECTS.camera.controls.enableKeys = CONFIG.camera.controls.enableKeys;
        OBJECTS.camera.controls.enableDamping = true;
        OBJECTS.camera.controls.rotateSpeed = 0.25;
        OBJECTS.camera.controls.minDistance =
          CONFIG.camera.controls.minDistance;
        OBJECTS.camera.controls.maxDistance =
          CONFIG.camera.controls.maxDistance;
      }
    } else {
      OBJECTS.camera.lookAt(0, 0, 0);
    }

    if (CONFIG.fog.enabled) {
      OBJECTS.scenes.mainScene.fog = new THREE.Fog(
        CONFIG.fog.color,
        CONFIG.fog.near,
        CONFIG.fog.far
      );
    }

    OBJECTS.camera.add(OBJECTS.audioListener);

    // OBJECTS.currentCamera = OBJECTS.camera

    // OBJECTS.scenes.mainScene.add(OBJECTS.camera)
    OBJECTS.scenes.uiScene.add(OBJECTS.uiCamera);

    // postprocessing and effects composer
    POSTPROCESSING.init();

    window.addEventListener(
      "resize",
      () => {
        TOOLS.adaptAllCameras();
      },
      true
    );

    TOOLS.adaptWindow(OBJECTS.currentCamera);

    if (CONFIG.release == "dev") {
      window.w = OBJECTS; // entities and components
      window.t = TOOLS; // static functions for building the game world
      window.f = FLAGS;
      window.c = CONFIG;
      window.THREE = THREE;
    }

    PRELOADER.init();
  },
};

export default APP;
